var search_history_key = 'fwshop_search_history_key';//搜索历史缓存key

/*
增加搜索关键词到历史记录
*/
function add_history(keyword) {
  if (typeof (keyword) == "undefined" || keyword.length == 0) { return; }
  var search_list = wx.getStorageSync(search_history_key);
  if (search_list) {
    if (search_list.indexOf(keyword) < 0) {
      search_list.unshift(keyword);
    }
    wx.setStorage({
      key: search_history_key,
      data: search_list,
      success: function () {
        return search_list;
      }
    })
  } else {
    search_list = [];
    search_list.push(keyword);
    wx.setStorage({
      key: search_history_key,
      data: search_list,
      success: function () {
        return search_list;
      }
    })
  }
}

/*
获得搜索历史记录
*/
function get_history() {
  var search_list = [];
  try {
    search_list = wx.getStorageSync(search_history_key);
    if (search_list) {
      return search_list;
    }
  } catch (e) {
    console.log('get history error');
  }
  return search_list;
}

/*
清空搜索历史记录
*/
function clear_history() {
  try {
    wx.removeStorageSync(search_history_key)
  } catch (e) {
    console.log('clear history error!')
  }
}

module.exports = {
  add_history: add_history,
  get_history: get_history,
  clear_history: clear_history
}
